-
Notifications
You must be signed in to change notification settings - Fork 1k
generate xpaths for namespaced elements #962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 5c3ab26 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Summary
This PR adds support for generating XPaths that properly handle namespaced XML/HTML elements. The core issue was that Stagehand's XPath generation was creating invalid paths for elements with namespaced tag names (containing colons, like foo:bar
).
The change modifies the generateAccessibilityTreeXPath
function in lib/a11y/utils.ts
to detect when a tag name contains a colon and switches from the standard XPath syntax (foo:bar[index]
) to the W3C XPath specification-compliant syntax (*[name()='foo:bar'][index]
). This ensures that the generated XPaths can actually resolve in browsers, as standard XPath parsers don't handle namespaced elements without proper namespace declarations in the XPath context.
This fix is crucial for Stagehand's DOM traversal functionality when working with web applications that use XML namespaces or custom namespaced elements. The change is isolated to a single utility function that's used throughout Stagehand's accessibility tree processing, which means it will improve XPath generation across all of Stagehand's core features (act, extract, observe).
Confidence score: 4/5
- This PR is safe to merge with minimal risk as it addresses a specific XPath generation bug
- Score reflects a targeted fix with clear logic and proper XPath specification compliance
- Pay close attention to
lib/a11y/utils.ts
to ensure the colon detection logic is robust
1 file reviewed, no comments
61cb279
to
5c3ab26
Compare
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @browserbasehq/[email protected] ### Patch Changes - [#951](#951) [`f45afdc`](f45afdc) Thanks [@miguelg719](https://github.com/miguelg719)! - Patch GPT-5 new api format - [#954](#954) [`261bba4`](261bba4) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - add support for shadow DOMs (open & closed mode) when experimental: true - [#944](#944) [`8de7bd8`](8de7bd8) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Bump zod version compatibility and add pathing spec - [#919](#919) [`3d80421`](3d80421) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - enable scrolling inside of iframes - [#963](#963) [`0ead63d`](0ead63d) Thanks [@tkattkat](https://github.com/tkattkat)! - Properly handle images in evaluator + clean up response parsing logic - [#961](#961) [`8422828`](8422828) Thanks [@tkattkat](https://github.com/tkattkat)! - Add more evals for stagehand agent - [#946](#946) [`b769206`](b769206) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix: unable to act on/get content from some same process iframes - [#962](#962) [`72d2683`](72d2683) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - handle namespaced elements in xpath build step ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies \[[`f45afdc`](f45afdc), [`261bba4`](261bba4), [`8de7bd8`](8de7bd8), [`3d80421`](3d80421), [`0ead63d`](0ead63d), [`8422828`](8422828), [`b769206`](b769206), [`72d2683`](72d2683)]: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies \[[`f45afdc`](f45afdc), [`261bba4`](261bba4), [`8de7bd8`](8de7bd8), [`3d80421`](3d80421), [`0ead63d`](0ead63d), [`8422828`](8422828), [`b769206`](b769206), [`72d2683`](72d2683)]: - @browserbasehq/[email protected] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
why
nodeName
&localName
below:/html/body/foo:bar/div/select
/html/body/*[name()='foo:bar']/div/select
what changed
:
in the xpath building step. if we find this, we format the path segment as defined above (/*[name()='foo:bar']
)test plan
namespace_xpath
)act
evalsextract
evalsobserve
evals